package com.quo.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.DeleteMapping;

import com.quo.dto.ProductPlanDto;
import com.quo.dto.ProductsPlanDto;
import com.quo.dto.QuoDto;
import com.quo.dto.QuoInfo;
import com.quo.dto.QuotesDto;
import com.quo.entity.QuoDetail;
import com.quo.entity.Quote;
import com.quo.entity.QuoteDetail;

/** 

* @author zhoumin

*/
@Mapper
public interface QuoteMapper {

	@Select("select pid from quotedetail")
	Long[] getPidsByQuoteDetail();

	@Select("select q.qid,q.abst,q.qdate,q.totalamount,c.cname,qs.qsname,e.ename from quote q "
			+ "left join customer c on q.cid=c.cid left join qstatus qs on q.qsid=qs.qsid left join "
			+ "emp e on q.ceno=e.eno")
	List<QuotesDto> getQuoteList();

	@Select("select q.qid,q.qdate,q.abst,q.totalamount,q.payment,q.expiry,q.edd,qs.qsname,e.ename,c.cid,c.cname,c.address,"
			+ "c.zip,c.tel,c.contact,c.email,c.fax from quote q left join "
			+ "qstatus qs on q.qsid=qs.qsid left join emp e on q.ceno=e.eno left join customer c "
			+ "on q.cid=c.cid where qid=#{qid}")
	QuoDto getQuoDto(Long qid);

	/*@Insert("Insert into quote(qdate,abst,payment,expiry,edd,cid,totalamount,ceno) values(#{qdate},#{abst},"
			+ "#{payment},#{expiry},#{edd},#{cid},#{totalamount},#{ceno})")*/
	void addQuote(Quote quote);

	/*@Insert("Insert into quotedetail(pid,tax,discount,payment,quantity,pname,ptype,pseries,price,pstatus,qid) "
			+ "values(#{pid},#{tax},#{discount},#{payment},#{quantity},#{pname},#{ptype},#{pseries},#{price},"
			+ "#{pstatus},#{qid})")*/
	void addQuoteDetail(QuoteDetail qd);

	void addAllQuoteDetail(List<QuoteDetail> quoteDetailList);

	@Select("select q.qid,q.abst,q.qdate,q.totalamount,c.cname,qs.qsname,e.ename from quote q "
			+ "left join customer c on q.cid=c.cid left join qstatus qs on q.qsid=qs.qsid left join "
			+ "emp e on q.ceno=e.eno where eno=#{eno}")
	List<QuotesDto> getQuoteListForSelf(Integer eno);

	/*@Delete("delete q.*,qd.* from quote q left join quotedetail qd on q.qid=qd.qid where qid=#{qid}")
	Boolean delQuoInfo(Long qid);*/

	@Delete("delete from quote where qid=#{qid}")
	void delQuote(Long qid);

	@Delete("delete from quotedetail where qid=#{qid}")
	void delQuoteDetail(Long qid);
	
	@Select("select pid,pname,quantity,discount,price from quotedetail where qid=#{qid}")
	List<QuoDetail> getQuoDetailList(Long qid);

	//获取特定报价单信息（个人）
	@Select("select q.qid,q.qdate,q.abst,q.totalamount,q.payment,q.expiry,q.edd,qs.qsname,e.ename,c.cid,c.cname,c.address,"
			+ "c.zip,c.tel,c.contact,c.email,c.fax from quote q left join "
			+ "qstatus qs on q.qsid=qs.qsid left join emp e on q.ceno=e.eno left join customer c "
			+ "on q.cid=c.cid where qid=#{qid} and eno=#{eno}")
	QuoDto getQuoDtoForSelf(@Param("qid")Long qid, @Param("eno")Integer eno);

	List<QuotesDto> selectByQids(Long[] qids);

	@Select("select * from quote")
	List<Quote> getQuotePrint();

	//获取所有待一级审核报价单信息
	@Select("select q.qid,q.abst,q.qdate,q.totalamount,c.cname,qs.qsname,e.ename from quote q "
			+ "left join customer c on q.cid=c.cid left join qstatus qs on q.qsid=qs.qsid left join "
			+ "emp e on q.ceno=e.eno where q.qsid=3")
	List<QuotesDto> getQListForFirstExamine();

	void setAllQuotePass(Long[] qids);

	void setAllQuoteDeny(Long[] qids);

	@Update("update quote set qsid=4 where qid=#{qid}")
	void setQuotePass(Long qid);

	@Update("update quote set qsid=6 where qid=#{qid}")
	void setQuoteDeny(Long qid);

	//获取生产计划审核报价单信息
	@Select("select qd.pid,qd.pname,qd.ptype,qd.pseries,qd.price,qd.quantity,"
			+ "p.stock,e.ename,q.qdate from quotedetail qd left join product p on qd.pid=p.pid "
			+ "left join quote q on qd.qid=q.qid left join emp e on q.ceno=e.eno where q.qsid=5")
	List<ProductsPlanDto> getQListForProduce();

	@Select("select e.ename,q.qdate,q.expiry,qd.quantity,qd.pid,qd.pname,qd.ptype,"
			+ "qd.pseries,qd.price,p.stock,p.connection,p.pInterface,p.noise,p.bass,"
			+ "p.waterproof,p.mic,p.packageInfo from quotedetail qd left join quote q"
			+ " on qd.qid=q.qid left join emp e on q.ceno=e.eno left join product p on "
			+ "qd.pid=p.pid where qd.pid=#{pid}")
	ProductPlanDto getProductPlanDto(Long pid);

	void setAllProductPlanPass(Long[] qids);

	void setAllProductPlanDeny(Long[] qids);

	@Update("update quote set qsid=9 where qid=#{qid}")
	void setProductPlanPass(Long qid);

	@Update("update quote set qsid=6 where qid=#{qid}") 
	void setProductPlanDeny(Long qid);


}